﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KPIS.GERP.GBMS.APP_UI.Common;
using KPIS.GERP.GBMS.APP_UI.UserControls;
using KPIS.GERP.GBMS.BLL;
using KPIS.GERP.GBMS.MODEL;
using KPIS.GERP.GHRMS.BLL;
using Telerik.Web.UI;

namespace KPIS.GERP.GBMS.APP_UI.Form
{
    public partial class BGM_ProjectNew : System.Web.UI.Page
    {
        //private IProject projectBLL;
        //private ComboBLL comboBLL = new ComboBLL();
        //private SystemTypeYearBLL sysTypeYearBLL = new SystemTypeYearBLL();
        //private EmployeeBLL empBLL = new EmployeeBLL();
        //private SystemControlBLL scBLL = new SystemControlBLL();
        //private OptionTypeBLL optionTypeBLL = new OptionTypeBLL();
        //private SourceBLL source = new SourceBLL();
        //private RequestPass requestPass = new RequestPass();
        //private Main main = new Main();
        //private SsMng ssMng = new SsMng();

        //protected void Page_Load(object sender, EventArgs e)
        //{
        //    projectBLL = Project_Factory.GetProjectFactory(Request.QueryString["type"]);

        //    if (!IsPostBack)
        //    {
        //        lblType.Text = Request.QueryString["type"];
        //        lbBgmYear.Text = Request.QueryString["year"];
        //        lbProjDate.Text = DateTime.Today.ToString("dd MMMM yyyy", new CultureInfo("th-TH"));

        //        LoadMasterData();

        //        Session["Approve"] = false;
        //        ChangeMode(Main.EditMode.View);
        //    }
        //}

        //#region Main Region

        //private void LoadDtgProj()
        //{
        //    string strUserJobSiteID = ssMng.WorkingJobSiteSeq;
        //    bool blnIsStrategyJobSite = (strUserJobSiteID == scBLL.JobSiteStrategyID());

        //    DataTable dtb = projectBLL.GetDataProjectByYearAndJobSite(lbBgmYear.Text, lblJobSiteSeq.Text, "", "");
        //    dtgProj.DataSource = dtb;
        //}

        //private void LoadMasterData()
        //{
        //    ddlStrategy.DataSource = comboBLL.ListStrategy(lbBgmYear.Text);
        //    ddlStrategy.DataBind();

        //    ddlJobSite.DataSource = comboBLL.ListJobsite();
        //    ddlJobSite.DataBind();
        //    ddlJobSite.SelectedValue = lblJobSiteSeq.Text;

        //    ddlProjType.DataSource = comboBLL.ListProjectTypeWithNewProjectComeFirst();
        //    ddlProjType.DataBind();

        //    ddlProjScope.DataSource = comboBLL.ListScope();
        //    ddlProjScope.DataBind();
        //}

        //private void LoadGridSource(string strProjectSeq)
        //{
        //    gridSource.DataSource = projectBLL.GetDataProjectBySequenceForGridIn(strProjectSeq);
        //    gridSource.DataBind();
        //}

        //private void LoadGridManager(string strProjectSeq)
        //{
        //    gridManager.DataSource = projectBLL.GetDataProjectManager(strProjectSeq);
        //    gridManager.DataBind();
        //}

        //private void LoadRepeaterJobSitePass(string strProjectSeq)
        //{
        //    repeaterJobSitePass.DataSource = requestPass.GetData(strProjectSeq);
        //    repeaterJobSitePass.DataBind();
        //}

        //private void LoadOption(string strProjectSeq)
        //{
        //    IList<OptionTypeInfo> optionTypeInfoList = optionTypeBLL.Search(-1);

        //    foreach (OptionTypeInfo info in optionTypeInfoList)
        //    {
        //        string strBgmOptionTypeSeq = info.Sequence.ToString();

        //        BGM_ProjectOption userControl = (BGM_ProjectOption)Page.LoadControl("~/APP_UI/UserControls/BGM_ProjectOption.ascx");
        //        userControl.LoadUserControlData(strBgmOptionTypeSeq, strProjectSeq, lblType.Text);

        //        PanelOption.Controls.Add(userControl);
        //    }
        //}

        //private void ChangeMode(Main.EditMode mode)
        //{
        //    bool blnApproved = (bool)Session["Approve"] && (lblType.Text != "pj");

        //    switch (mode)
        //    {
        //        case Main.EditMode.AddNew :
        //            btnAddNew.Visible = false;
        //            btnEdit.Visible = false && !blnApproved;
        //            btnDelete.Visible = false && !blnApproved;
        //            btnPrintPreview.Visible = false;
        //            btnApprove.Visible = false && !blnApproved;
        //            btnSaveAddNew.Visible = true && !blnApproved;
        //            btnSaveEdit.Visible = false && !blnApproved;
        //            btnCancel.Visible = true;

        //            gridSource.Columns[0].Visible = false;
        //            btnExpPlan.Enabled = false;
        //            btnManager.Enabled = false;
        //            btnWorkPlan.Enabled = false;
        //            btnKPI.Enabled = false;
        //            pnlAssignProjectPopup.Visible = false;//(lblType.Text == "pj");

        //            pnlView.Visible = false;
        //            pnlInfo.Visible = true;

        //            ViewInfo(false);
        //            break;
        //        case Main.EditMode.Edit:
        //            btnAddNew.Visible = false;
        //            btnEdit.Visible = false && !blnApproved;
        //            btnDelete.Visible = true && !blnApproved;
        //            btnPrintPreview.Visible = true;
        //            btnApprove.Visible = true && !blnApproved;
        //            btnSaveAddNew.Visible = false && !blnApproved;
        //            btnSaveEdit.Visible = true && !blnApproved;
        //            btnCancel.Visible = true;

        //            gridSource.Columns[0].Visible = true;
        //            btnExpPlan.Enabled = true;
        //            btnManager.Enabled = true;
        //            btnWorkPlan.Enabled = true;
        //            btnKPI.Enabled = true;
        //            pnlAssignProjectPopup.Visible = (lblType.Text == "pj");

        //            pnlView.Visible = false;
        //            pnlInfo.Visible = true;

        //            ViewInfo(false);
        //            break;
        //        case Main.EditMode.View:
        //            btnAddNew.Visible = true;
        //            btnEdit.Visible = false && !blnApproved;
        //            btnDelete.Visible = false && !blnApproved;
        //            btnPrintPreview.Visible = false;
        //            btnApprove.Visible = false && !blnApproved;
        //            btnSaveAddNew.Visible = false && !blnApproved;
        //            btnSaveEdit.Visible = false && !blnApproved;
        //            btnCancel.Visible = false;

        //            pnlView.Visible = true;
        //            pnlInfo.Visible = false;
        //            break;
        //        case Main.EditMode.Info:
        //            btnAddNew.Visible = false;
        //            btnEdit.Visible = true && !blnApproved;
        //            btnDelete.Visible = true && !blnApproved;
        //            btnPrintPreview.Visible = true;
        //            btnApprove.Visible = true && !blnApproved;
        //            btnSaveAddNew.Visible = false && !blnApproved;
        //            btnSaveEdit.Visible = false && !blnApproved;
        //            btnCancel.Visible = true;

        //            gridSource.Columns[0].Visible = true;
        //            btnExpPlan.Enabled = true;
        //            btnManager.Enabled = true;
        //            btnWorkPlan.Enabled = true;
        //            btnKPI.Enabled = true;
        //            pnlAssignProjectPopup.Visible = (lblType.Text == "pj");

        //            pnlView.Visible = false;
        //            pnlInfo.Visible = true;

        //            ViewInfo(true);
        //            break;
        //    }
        //}

        //private void ViewInfo(bool blnViewInfo)
        //{
        //    lbProjDate.Visible = blnViewInfo;
        //    txtProjDate.Visible = !blnViewInfo;
        //    lbProjName.Visible = blnViewInfo;
        //    txtProjName.Visible = !blnViewInfo;
        //    lbProjStrat.Visible = blnViewInfo;
        //    ddlStrategy.Visible = !blnViewInfo;
        //    lbProjJobSiteMain.Visible = blnViewInfo;
        //    ddlJobSite.Visible = !blnViewInfo;
        //    btnJobSitePass.Visible = !blnViewInfo;

        //    lblStartDate.Visible = blnViewInfo;
        //    txtStartDate.Visible = !blnViewInfo;
        //    lblEndDate.Visible = blnViewInfo;
        //    txtEndDate.Visible = !blnViewInfo;
        //    lbProjType.Visible = blnViewInfo;
        //    ddlProjType.Visible = !blnViewInfo;
        //    lbProjScope.Visible = blnViewInfo;
        //    ddlProjScope.Visible = !blnViewInfo;

        //    lbProjReason.Visible = blnViewInfo;
        //    txtProjReason.Visible = !blnViewInfo;
        //    lbProjObject.Visible = blnViewInfo;
        //    txtProjObject.Visible = !blnViewInfo;
        //    lbProjTarget.Visible = blnViewInfo;
        //    txtProjTarget.Visible = !blnViewInfo;
        //    lbProjPlan.Visible = blnViewInfo;
        //    txtProjPlan.Visible = !blnViewInfo;
        //    lbProjRemark.Visible = blnViewInfo;
        //    txtProjRemark.Visible = !blnViewInfo;
        //}

        //private void ClearControl()
        //{
        //    lbProjDate.Text = "";
        //    txtProjDate.Text = "";
        //    lbProjName.Text = "";
        //    txtProjName.Text = "";
        //    lbProjStrat.Text = "";
        //    ddlStrategy.SelectedIndex = 0;
        //    lbProjJobSiteMain.Text = "";
        //    ddlJobSite.SelectedValue = lblJobSiteSeq.Text;

        //    lblStartDate.Text = "";
        //    txtStartDate.SetValue("");
        //    lblEndDate.Text = "";
        //    txtEndDate.SetValue("");
        //    lbProjType.Text = "";
        //    ddlProjType.SelectedIndex = 0;
        //    lbProjScope.Text = "";
        //    ddlProjScope.SelectedIndex = 0;

        //    lbProjReason.Text = "";
        //    txtProjReason.Text = "";
        //    lbProjObject.Text = "";
        //    txtProjObject.Text = "";
        //    lbProjTarget.Text = "";
        //    txtProjTarget.Text = "";
        //    lbProjPlan.Text = "";
        //    txtProjPlan.Text = "";
        //    lbProjRemark.Text = "";
        //    txtProjRemark.Text = "";

        //    LoadGridManager("-1");
        //    LoadGridSource("-1");
        //    LoadRepeaterJobSitePass("-1");
        //    LoadOption("-1");
        //}

        //private void SetProjectInfo()
        //{
        //    try
        //    {
        //        DataTable dt = projectBLL.GetDataProjectBySequence(lblProjectSeq.Text);

        //        if (dt.Rows.Count > 0)
        //        {
        //            lbBgmYear.Text = dt.Rows[0]["bgm_year"].ToString();
        //            lbProjDate.Text = Convert.ToDateTime(dt.Rows[0]["bgm_prj_date"]).ToString("dd MMMM yyyy", new CultureInfo("th-TH"));
        //            lbProjName.Text = dt.Rows[0]["bgm_prj_name"].ToString();
        //            lbProjStrat.Text = dt.Rows[0]["bgm_strat_list_name"].ToString();
        //            lbProjJobSiteSeq.Text = dt.Rows[0]["job_site_seq"].ToString();
        //            lbProjJobSite.Text = dt.Rows[0]["job_site_name"].ToString();
        //            lbProjJobSiteMain.Text = dt.Rows[0]["job_site_name_main"].ToString();
        //            lbProjType.Text = dt.Rows[0]["prj_type_name"].ToString();
        //            lbProjScope.Text = dt.Rows[0]["bgm_scope_name"].ToString();

        //            lbProjReason.Text = (dt.Rows[0]["bgm_prj_reason"].ToString().Length > 0)
        //                ? main.WebString(dt.Rows[0]["bgm_prj_reason"].ToString())
        //                : "-";
        //            lbProjObject.Text = (dt.Rows[0]["bgm_prj_object"].ToString().Length > 0)
        //                ? main.WebString(dt.Rows[0]["bgm_prj_object"].ToString())
        //                : "-";
        //            lbProjTarget.Text = (dt.Rows[0]["bgm_prj_target"].ToString().Length > 0)
        //                ? main.WebString(dt.Rows[0]["bgm_prj_target"].ToString())
        //                : "-";
        //            lbProjPlan.Text = (dt.Rows[0]["bgm_prj_plan"].ToString().Length > 0)
        //                ? main.WebString(dt.Rows[0]["bgm_prj_plan"].ToString())
        //                : "-";
        //            lbProjRemark.Text = (dt.Rows[0]["bgm_prj_remark"].ToString().Length > 0)
        //                ? main.WebString(dt.Rows[0]["bgm_prj_remark"].ToString())
        //                : "-";
        //            lblStartDate.Text = Convert.ToDateTime(dt.Rows[0]["date_start"]).ToString("dd/MM/yyyy", new CultureInfo("th-TH"));
        //            lblEndDate.Text = Convert.ToDateTime(dt.Rows[0]["date_end"]).ToString("dd/MM/yyyy", new CultureInfo("th-TH"));

        //            lbBgmYear.Text = dt.Rows[0]["bgm_year"].ToString();
        //            txtProjDate.Text = dt.Rows[0]["bgm_prj_date"].ToString();
        //            txtProjName.Text = dt.Rows[0]["bgm_prj_name"].ToString();
        //            ddlStrategy.SelectedValue = dt.Rows[0]["bgm_strat_list_seq"].ToString();
        //            ddlStrategy.Text = dt.Rows[0]["bgm_strat_list_name"].ToString();
        //            ddlJobSite.SelectedValue = dt.Rows[0]["job_site_seq_main"].ToString();
        //            ddlJobSite.Text = dt.Rows[0]["job_site_name_main"].ToString();
        //            ddlProjType.SelectedValue = dt.Rows[0]["prj_type_seq"].ToString();
        //            ddlProjType.Text = dt.Rows[0]["prj_type_name"].ToString();
        //            ddlProjScope.SelectedValue = dt.Rows[0]["bgm_prj_scope"].ToString();
        //            ddlProjScope.Text = dt.Rows[0]["bgm_scope_name"].ToString();

        //            txtProjReason.Text = (dt.Rows[0]["bgm_prj_reason"].ToString().Length > 0)
        //                ? main.WebString(dt.Rows[0]["bgm_prj_reason"].ToString())
        //                : "-";
        //            txtProjObject.Text = (dt.Rows[0]["bgm_prj_object"].ToString().Length > 0)
        //                ? main.WebString(dt.Rows[0]["bgm_prj_object"].ToString())
        //                : "-";
        //            txtProjTarget.Text = (dt.Rows[0]["bgm_prj_target"].ToString().Length > 0)
        //                ? main.WebString(dt.Rows[0]["bgm_prj_target"].ToString())
        //                : "-";
        //            txtProjPlan.Text = (dt.Rows[0]["bgm_prj_plan"].ToString().Length > 0)
        //                ? main.WebString(dt.Rows[0]["bgm_prj_plan"].ToString())
        //                : "-";
        //            txtProjRemark.Text = (dt.Rows[0]["bgm_prj_remark"].ToString().Length > 0)
        //                ? main.WebString(dt.Rows[0]["bgm_prj_remark"].ToString())
        //                : "-";
        //            txtStartDate.SetValue(dt.Rows[0]["date_start"]);
        //            txtEndDate.SetValue(dt.Rows[0]["date_end"]);

        //            DataTable dtb = comboBLL.GetInfoFromRefSeq(lblProjectSeq.Text, "bgm_request");

        //            if (dtb.Rows.Count > 0)
        //            {
        //                lblReplyRemark.Text = dtb.Rows[0]["remarks"].ToString();
        //                PanelReplyRemarks.Visible = (lblReplyRemark.Text.Length > 0);
        //            }
        //            else
        //            {
        //                lblReplyRemark.Text = "";
        //                PanelReplyRemarks.Visible = false;
        //            }

        //            LoadGridManager(lblProjectSeq.Text);
        //            LoadGridSource(lblProjectSeq.Text);
        //            LoadRepeaterJobSitePass(lblProjectSeq.Text);
        //            LoadOption(lblProjectSeq.Text);

        //            Session["ProjectName"] = lbProjName.Text;

        //            //============ เตือนไม่มีข้อมูล ===============
        //            DataTable dtb2;
        //            dtb2 = projectBLL.GetDataProjectActivityPlan(lblProjectSeq.Text);
        //            lbWorkPlanEmpty.Visible = (dtb2.Rows.Count == 0);
        //            dtb2 = projectBLL.GetDataProjectExpensePlan(lblProjectSeq.Text, null);
        //            lbExpPlanEmpty.Visible = (dtb2.Rows.Count == 0);
        //            dtb2 = projectBLL.GetDataProjectKPI(lblProjectSeq.Text);
        //            lbKPIEmpty.Visible = (dtb2.Rows.Count == 0);
        //            //=======================================
        //        }
        //    }
        //    catch (Exception e)
        //    {
        //        throw e;
        //    }
        //}

        //#endregion

        //#region Control's Event Region

        //protected void btnAddNew_Click(object sender, EventArgs e)
        //{
        //    ChangeMode(Main.EditMode.AddNew);
        //}

        //protected void btnEdit_Click(object sender, EventArgs e)
        //{
        //    ChangeMode(Main.EditMode.Edit);
        //}

        //protected void Delete_Click(object sender, EventArgs e)
        //{
        //    LoadDtgProj();
        //    dtgProj.DataBind();
        //    ClearControl();
        //    ChangeMode(Main.EditMode.View);
        //}

        //protected void btnPrintPreview_Click(object sender, EventArgs e)
        //{

        //}

        //protected void btnSaveAddNew_Click(object sender, EventArgs e)
        //{
        //    LoadDtgProj();
        //    dtgProj.DataBind();
        //    ClearControl();
        //    ChangeMode(Main.EditMode.View);
        //}

        //protected void btnSaveEdit_Click(object sender, EventArgs e)
        //{
        //    LoadDtgProj();
        //    dtgProj.DataBind();
        //    ClearControl();
        //    ChangeMode(Main.EditMode.View);
        //}

        //protected void btnCancel_Click(object sender, EventArgs e)
        //{
        //    LoadDtgProj();
        //    dtgProj.DataBind();
        //    ClearControl();
        //    ChangeMode(Main.EditMode.View);
        //}

        //protected void RadAjaxManager1_AjaxRequest(object sender, Telerik.Web.UI.AjaxRequestEventArgs e)
        //{
        //    string strArg = e.Argument;
        //    string strProjectSeq = lblProjectSeq.Text;

        //    if (strArg == "Approve")
        //    {
        //        string strRecordStatus = projectBLL.GetDataProjectBySequence(lblProjectSeq.Text).Rows[0]["record_status"].ToString();

        //        if (strRecordStatus == "P")
        //        {
        //            ChangeMode(Main.EditMode.Info);
        //        }
        //    }
        //    else if (strArg == "JobSitePass")
        //    {
        //        LoadRepeaterJobSitePass(strProjectSeq);
        //    }
        //    else if (strArg == "Manager")
        //    {
        //        LoadGridManager(strProjectSeq);
        //    }
        //    else if (strArg == "Source")
        //    {
        //        LoadGridSource(strProjectSeq);
        //    }
        //    else if (strArg == "Option")
        //    {
        //        LoadOption(strProjectSeq);
        //    }
        //    else
        //    {
        //        SetProjectInfo();
        //    }
        //}

        //protected void dtgProj_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        //{
        //    LoadDtgProj();
        //}

        //protected void dtgProj_EditCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        //{
        //    GridDataItem item = (GridDataItem)e.Item;
        //    string strBgmProjSeq = item["Sequence"].Text;
        //    string strRecordStatus = item["record_status"].Text;
        //    string strRecordStatusName = item["record_status_name"].Text;

        //    Session["Approve"] = (strRecordStatus == "P") || (strRecordStatus == "A");
        //    lblApproved.Text = strRecordStatusName;
        //    lblProjectSeq.Text = strBgmProjSeq;
        //    Session["ProjectID"] = strBgmProjSeq;
        //    SetProjectInfo();
        //    ChangeMode(Main.EditMode.Edit);
        //}

        //protected void repeaterJobSitePass_ItemCommand(object source, RepeaterCommandEventArgs e)
        //{
        //    if (e.CommandName == "Delete")
        //    {
        //        string strProjectSeq = lblProjectSeq.Text;
        //        Label lblBgmReqPassSeq = (Label)e.Item.FindControl("lblBgmReqPassSeq");
        //        requestPass.Delete(lblBgmReqPassSeq.Text, ssMng.EmpSeq);

        //        repeaterJobSitePass.DataSource = requestPass.GetData(strProjectSeq);
        //        repeaterJobSitePass.DataBind();
        //    }
        //}

        //#endregion
    }
}